package game;

import highscore.Highscore;

import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGAnimationElement;

/**
 * This class handles events thrown by the SVG-Menu
 * as cursor movement or choosing something and 
 * does the respective actions in the SVG-File.
 */
public class MySVGEventListener implements SVGEventListener {
	
	/** The document. */
	private Document doc;
	
	/** The gameMidlet. */
	private GameMIDlet gameMidlet;
	
	/** The actually hovered Element (as "Neues Spiel"). */
	private int aktElem = 0;		//aktuelles Element
	
	private Highscore highscore;
	
	/**
	 * Instantiates a new my svg event listener.
	 *
	 * @param svgImage the svgImage
	 * @param gameMidlet the parent gameMidlet
	 */
	public MySVGEventListener(SVGImage svgImage, GameMIDlet gameMidlet, Highscore highscore) {
		this.doc = svgImage.getDocument();
		this.gameMidlet = gameMidlet;
		this.highscore = highscore;
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#keyPressed(int)
	 */
	public void keyPressed(int keystate) {
		switch(keystate) {
			case -1: menueNachOben();			//nach oben
				break;
			case -2: menueNachUnten();			//nach unten
				break;
			case -5: menueEnter();				//"enter" (Mitteltaste)
				break;
		}
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#keyReleased(int)
	 */
	public void keyReleased(int keystate) {
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#hideNotify()
	 */
	public void hideNotify() {
		
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#pointerPressed(int, int)
	 */
	public void pointerPressed(int arg0, int arg1) {
		
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#pointerReleased(int, int)
	 */
	public void pointerReleased(int arg0, int arg1) {
		
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#showNotify()
	 */
	public void showNotify() {
		
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.m2g.SVGEventListener#sizeChanged(int, int)
	 */
	public void sizeChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Selection should move up.
	 */
	private void menueNachOben() {
		switch(aktElem) {
			case 0: 			//schon ganz oben
				break;
			case 1: getElementById("menueItem1a").beginElementAt(0);
					getElementById("menueItem2i").beginElementAt(0);
					aktElem--;
				break;
			case 2: getElementById("menueItem2a").beginElementAt(0);
					getElementById("menueItem3i").beginElementAt(0);
					aktElem--;
				break;
		}
	}
	
	/**
	 * Selection should move down.
	 */
	private void menueNachUnten() {
		switch(aktElem) {
			case 0: getElementById("menueItem1i").beginElementAt(0);
					getElementById("menueItem2a").beginElementAt(0);
					aktElem++;
				break;
			case 1: getElementById("menueItem2i").beginElementAt(0);
					getElementById("menueItem3a").beginElementAt(0);
					aktElem++;
				break;
			case 2: 			//schon ganz unten
				break;
		}
	}
	
	/**
	 * Menue enter.
	 */
	private void menueEnter() {
		switch(aktElem) {
		case 0: gameMidlet.showGame();
			break;
		case 1: gameMidlet.showNewHighscoreScreen(highscore.ReadHighscore());
			break;
		case 2: gameMidlet.showHelp();
			break;
	}
	}
	
	/**
	 * Gets the SVGAnimationElement by id.
	 *
	 * @param id the id as String
	 * @return the SVGAnimationElement
	 */
	private SVGAnimationElement getElementById(String id) {
		return (SVGAnimationElement)doc.getElementById(id);
	}
}